package com.zw.webSocket;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;


/**
 * 创建人:zhuwei
 * 邮箱:121203654@qq.com
 * 创建时间:2019-01-14 17:17
 */

@Controller
public class WebSocketController {
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    /**
     * 接收客户端发来的消息并推送消息给指定用户
     * 这里模拟的是app或者小程序登录后马上得到用户有多少条消息未读
     */
    @MessageMapping("/message")
    public void handleSubscribe(String userId) {
        System.out.println(userId);
        //未读消息数为10条
        Integer count=10;
        simpMessagingTemplate.convertAndSendToUser(userId,"/count",count);
    }

    /**
     * 同步服务器时间
     */
    @MessageMapping("/time")//接受消息的主题
    @SendTo("/topic/time")//推送消息的主题
    public void sendMessage(){
        simpMessagingTemplate.convertAndSend("/topic/time",new Date().getTime());
    }


    @RequestMapping("/index")
    public String index(){
        return "demo";
    }
}
